<template>
  <!-- 这个类用于进行raycasting（光线投射）。 光线投射用于进行鼠标拾取（在三维空间中计算出鼠标移过了什么物体）。 -->
  <div class="different-faces">
    <div id="scene"></div>
    <el-button type="primary" class="create-btn" @click="createCube"
      >创建视频纹理方块</el-button
    >
    <video
      id="testVideo"
      loop
      crossOrigin="anonymous"
      playsinline
      style="display: none"
    >
      <source
        src="@/assets/video/testVideo.mp4"
        type='video/mp4; codecs="avc1.42E01E, mp4a.40.2"'
      />
    </video>
  </div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";

let scene;
let camera;
let renderer;
let controls;
export default {
  data() {
    return {
      cube: null,
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    // 初始化
    init() {
      this.createScene(); // 创建场景
      this.createLight(); // 创建光源
      this.createCamera(); // 创建相机
      this.createRender(); // 创建渲染器
      this.createControls(); // 创建控件对象
      this.render(); // 渲染
    },
    // 创建场景
    createScene() {
      scene = new THREE.Scene();
    },
    // 创建方块
    createCube() {
      const cubeGeom = new THREE.BoxGeometry(16, 2, 9);

      let materialFaces = [];
      for (let i = 0; i < 6; i++) {
        materialFaces.push(new THREE.MeshPhongMaterial());
      }

      let video = document.getElementById("testVideo");
      video.play();
      let texture = new THREE.VideoTexture(video);
      texture.minFilter = THREE.LinearFilter;
      texture.magFilter = THREE.LinearFilter;
      texture.format = THREE.RGBFormat;
      materialFaces[2].map = texture;

      this.cube = new THREE.Mesh(cubeGeom, materialFaces);
      scene.add(this.cube);
    },
    // 创建光源
    createLight() {
      // 环境光
      const ambientLight = new THREE.AmbientLight(0xffffff, 0.3); // 创建环境光
      scene.add(ambientLight); // 将环境光添加到场景

      const spotLight = new THREE.SpotLight(0xffffff); // 创建聚光灯
      spotLight.position.set(-20, 40, -10);
      // spotLight.castShadow = true;
      scene.add(spotLight);
    },
    // 创建相机
    createCamera() {
      const element = document.getElementById("scene");
      const k = element.clientWidth / element.clientHeight; // 窗口宽高比
      // PerspectiveCamera( fov, aspect, near, far )
      camera = new THREE.PerspectiveCamera(35, k, 0.1, 1000);
      camera.position.set(-10, 10, 20); // 设置相机位置

      camera.lookAt(new THREE.Vector3(10, 0, 0)); // 设置相机方向
      scene.add(camera);
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer();
      renderer.setSize(element.clientWidth, element.clientHeight); // 设置渲染区域尺寸
      renderer.shadowMap.enabled = true; // 显示阴影
      renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      renderer.setClearColor(0x3f3f3f, 1); // 设置背景颜色
      element.appendChild(renderer.domElement);
    },
    render() {
      renderer.render(scene, camera);
      requestAnimationFrame(this.render);
    },
    // 创建控件对象
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
    },
  },
};
</script>
<style>
.different-faces {
  width: 100%;
  height: 100%;
  position: relative;
}
#scene {
  width: 100%;
  height: 100%;
}
.create-btn {
  position: absolute;
  top: 20px;
  left: 20px;
}
</style>

